package org.graduation.recruitment.controller.resume;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.graduation.recruitment.entity.pojo.resume.ProjectExperience;
import org.graduation.recruitment.service.resume.ProjectExperienceService;
import org.graduation.recruitment.utils.ResponseResult;
import org.graduation.recruitment.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/resume/project_experience")
@Slf4j
@Tag(name = "简历表中项目经验接口", description = "提供简历表中项目经验相关的CRUD操作")
@SecurityRequirement(name = "openApiSecurityScheme")
public class ProjectExperienceController {

    // 项目经验服务层自动注入
    @Autowired
    private ProjectExperienceService projectExperienceService;

    // Token工具类自动注入
    @Autowired
    private Token token;

    /**
     * 创建项目经验
     *
     * @param tokenStr          用户令牌
     * @param projectExperience 项目经验实体
     * @return 创建成功的项目经验实体
     */
    @PostMapping("/create")
    @Operation(summary = "创建项目经验接口")
    public ResponseResult createProjectExperience(@RequestHeader("Authorization") String tokenStr, @RequestBody ProjectExperience projectExperience) {
        // 通过Token获取用户ID，并设置到项目经验实体中
        projectExperience.setStudentId(token.getUidByToken(tokenStr));
        // 保存项目经验
        projectExperienceService.save(projectExperience);
        // 返回成功响应，携带创建的项目经验实体
        return ResponseResult.success(projectExperience);
    }

    /**
     * 更新项目经验
     *
     * @param projectExperience 需要更新的项目经验实体
     * @return 更新操作成功标识
     */
    @PostMapping("/update")
    @Operation(summary = "更新项目经验接口")
    public ResponseResult updateProjectExperience(@RequestBody ProjectExperience projectExperience) {
        // 更新项目经验信息
        projectExperienceService.updateById(projectExperience);
        // 返回成功响应
        return ResponseResult.success();
    }

    /**
     * 删除项目经验
     *
     * @param projectExperience 需要删除的项目经验实体
     * @return 删除操作成功标识
     */
    @PostMapping("/delete")
    @Operation(summary = "删除项目经验接口")
    public ResponseResult deleteProjectExperience(@RequestBody ProjectExperience projectExperience) {
        // 根据ID删除项目经验
        projectExperienceService.removeById(projectExperience.getId());
        // 返回成功响应
        return ResponseResult.success();
    }

    /**
     * 获取指定简历ID的所有项目经验
     *
     * @param resumeId 简历ID
     * @return 指定简历ID的所有项目经验列表
     */
    @GetMapping("/all")
    @Operation(summary = "查看对应resume id的所有项目经验接口")
    public ResponseResult getAllProjectExperience(@RequestParam Long resumeId) {
        // 根据简历ID获取所有项目经验
        return ResponseResult.success(projectExperienceService.getAllByResumeId(resumeId));
    }
}
